//
//  FinAppsNetwork.m
//  fonyou
//
//  Created by Ivan on 15/11/10.
//  Copyright 2010 Mobivery. All rights reserved.
//

#import "FinAppsNetwork.h"
#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"
#import "RESTConsumer.h"

@implementation FinAppsNetwork

// This is a singleton class, see below
static FinAppsNetwork *sharedDelegate = nil;
static ASINetworkQueue *myQueue = nil;

#pragma mark -
#pragma mark Private Methods

#pragma mark -
#pragma mark public methods

-(void)cancelAllRequests{
	for (ASIHTTPRequest *req in [myQueue operations]) {		
		[req clearDelegatesAndCancel];
	}	
}

-(void)addRequest:(ASIHTTPRequest*)aRequest{
	[myQueue addOperation:aRequest];
}

#pragma mark -
#pragma mark Singleton

// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info
// http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html

+ (FinAppsNetwork*)sharedInstance
{	
	@synchronized(self)
	{
		if (sharedDelegate == nil){
			sharedDelegate = [[self alloc] init]; // assignment not done here
			//ASINetworkQueue configuration
			myQueue = [[ASINetworkQueue queue] retain];
			[myQueue setShowAccurateProgress:NO];
			[myQueue setShouldCancelAllRequestsOnFailure:NO];
			[myQueue go];
		}
	}
	return sharedDelegate;
}

+ (id)allocWithZone:(NSZone *)zone
{
	@synchronized(self) 
	{
		if (sharedDelegate == nil)
		{
			sharedDelegate = [super allocWithZone:zone];
			return sharedDelegate;  // assignment and return on first allocation
		}
	}
	return nil; // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
	return self;
}

- (id)retain
{
	return self;
}

- (unsigned)retainCount
{
	return UINT_MAX;  // denotes an object that cannot be released
}

- (oneway void)release
{
	//do nothing
}

- (id)autorelease
{
	return self;
}

@end

